package com.vf.admin.miaodong.controller;

import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.dao.AppAuthDao;
import com.vf.admin.client.service.AppAuthService;
import com.vf.admin.client.service.AppAuthUserService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.SystemAlertService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.controller.AppDailyTasController;
import com.vf.admin.miaodong.entity.AppAuth;
import com.vf.admin.miaodong.entity.AppAuthUser;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/after/auth")
public class AfterAuthController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppAuthService appAuthService;
    @Autowired
    AppAuthDao appAuthDao;
    @Autowired
    AppAuthUserService appAuthUserService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    private AppDailyTasController appDailyTasController;
    @Autowired
    private SystemAlertService systemAlertService;

    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean, String authName, String authNameEn) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(authName)) {
                paramap.put("authName", authName);
            }
            if (StringEx.isNotNull(authNameEn)) {
                paramap.put("authNameEn", authNameEn);
            }
            List<AppAuth> list = appAuthService.page(paramap, splitPageBean);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/auth/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppAuth appAuth) {
        try {
            appAuthService.save(appAuth);

            return sucessJSON("success");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppAuth appAuth = appAuthService.get(id);

            request.setAttribute("obj", appAuth);
            return this.sucess("/web/after/auth/edit");
        } catch (Exception e) {
            logger.error("获取轮播图异常", e);
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result del(Integer id) {
        try {
            appAuthService.del(id);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/realAuthList")
    public String realAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.REAL_AUTH);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/realNameAuthList")
    public String realNameAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.REAL_NAME_AUTH);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/realNameAuthList");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/eduAuthList")
    public String eduAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.EDU_AUTH);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/eduAuthList");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/carAuthList")
    public String carAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.CAR_AUTH);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/carAuthList");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/houseAuthList")
    public String houseAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.HOUSE_AUTH);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/houseAuthList");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/voiceAuthList")
    public String voiceAuthList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(userId)) {
                paramap.put("userId", userId);
            }
            paramap.put("authNameEn", ClientConstants.UserAuth.VOICE);

            List<AppAuthUser> list = appAuthUserService.page(paramap, splitPageBean);
            for(AppAuthUser b : list) {
                AppUser appUser = appUserService.get(b.getUserId());
                b.setUser(appUser);
            }
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/realAuth/voiceAuthList");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @RequestMapping("/authSuccess")
    @ResponseBody
    public Result realAuthSuccess(Integer id) {
        AppAuthUser info = appAuthUserService.info(id);
        if(info.getStatus() == 2) {
            return this.errorJSON("认证已审核，请勿重复点击");
        }
        info.setStatus(2);
        info.setRejectReason(null);
        appAuthUserService.save(info);

        if(info.getAuthNameEn().equals(ClientConstants.UserAuth.EDU_AUTH)) {
            appUserService.updateEdu(info.getUserId(), info.getField2());
        }

        AppAuth appAuth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        appAuthUserService.doGiveDiamond(info.getUserId(), new BigDecimal(appAuth.getGive()));
        return this.sucessJSON("success");
    }

    /**
     * 审核驳回
     * @param id
     * @return
     */
    @RequestMapping("/authReject")
    @ResponseBody
    public Result realAuthReject(Integer id, String rejectReason) {
        AppAuthUser info = appAuthUserService.info(id);
        info.setStatus(3);
        info.setRejectReason(rejectReason);
        appAuthUserService.save(info);

        AppUser user = appUserService.get(info.getUserId());
        AppAuth auth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        systemAlertService.authNEWS(user.getTengxuncode(), auth.getAuthName(), rejectReason);
        return this.sucessJSON("success");
    }

    /**
     * 实名审核通过
     * @param id
     * @return
     */
    @RequestMapping("/realNameAuthSuccess")
    @ResponseBody
    public Result realNameAuthSuccess(Integer id) {
        AppAuthUser info = appAuthUserService.info(id);
        info.setStatus(2);
        info.setRejectReason(null);
        appAuthUserService.save(info);

        appUserService.updateRealName(info.getUserId(), info.getField1(), info.getField2());
        appDailyTasController.getAwardOnce(info.getUserId(), ClientConstants.TaskOnce.REAL_NAME);

        AppAuth appAuth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        appAuthUserService.doGiveDiamond(info.getUserId(), new BigDecimal(appAuth.getGive()));
        return this.sucessJSON("success");
    }

    /**
     * 实名审核驳回
     * @param id
     * @return
     */
    @RequestMapping("/realNameAuthReject")
    @ResponseBody
    public Result realNameAuthReject(Integer id, String rejectReason) {
        AppAuthUser info = appAuthUserService.info(id);
        info.setStatus(3);
        info.setRejectReason(rejectReason);
        appAuthUserService.save(info);

        AppUser user = appUserService.get(info.getUserId());
        AppAuth auth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        systemAlertService.authNEWS(user.getTengxuncode(), auth.getAuthName(), rejectReason);
        return this.sucessJSON("success");
    }

    /**
     * 语音签名审核通过
     * @param id
     * @return
     */
    @RequestMapping("/voiceAuthSuccess")
    @ResponseBody
    public Result voiceAuthSuccess(Integer id) {
        AppAuthUser info = appAuthUserService.info(id);
        if(info.getStatus() == 2) {
            return this.errorJSON("认证已审核，请勿重复点击");
        }
        info.setStatus(2);
        info.setRejectReason(null);
        appAuthUserService.save(info);

        AppUser appUser = appUserService.get(info.getUserId());
        appUser.setVoiceSignature(info.getImage());
        appUser.setVoiceSignatureSeconds(info.getField1());
        appUser.setVoiceSignatureParse(0);
        appUserService.update(appUser);

        appDailyTasController.getAwardOnce(info.getUserId(), ClientConstants.TaskOnce.VOICE_SIGNATURE);

        AppAuth appAuth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        appAuthUserService.doGiveDiamond(info.getUserId(), new BigDecimal(appAuth.getGive()));
        return this.sucessJSON("success");
    }

    /**
     * 语音签名审核驳回
     * @param id
     * @return
     */
    @RequestMapping("/voiceAuthReject")
    @ResponseBody
    public Result voiceAuthReject(Integer id, String rejectReason) {
        AppAuthUser info = appAuthUserService.info(id);
        info.setStatus(3);
        info.setRejectReason(rejectReason);
        appAuthUserService.save(info);

        AppUser user = appUserService.get(info.getUserId());
        AppAuth auth = appAuthDao.infoByNameEn(info.getAuthNameEn());
        systemAlertService.authNEWS(user.getTengxuncode(), auth.getAuthName(), rejectReason);
        return this.sucessJSON("success");
    }

    @RequestMapping("/gotoReject")
    public String selectHour(String url, Integer id) {
        try {
            request.setAttribute("url", url);
            request.setAttribute("id", id);
            return this.sucess("/web/after/realAuth/reject");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }
}
